﻿using System.Data.SqlClient;
using System.Text.RegularExpressions;
using com.fukefu.sql;

namespace com.fukefu.tools
{
    public class ArticleTemplet
    {
        IllegalWords iw = new IllegalWords();
        SqlServerOperate sql = new SqlServerOperate();
        ProductInformation pif = new ProductInformation();
        ArticleTag tag = new ArticleTag();
        KeywordType ktype = new KeywordType();
        SqlDataReader re;
        //获取一个标题
        public string Get(Title title)
        {
            string mid = "", result = "";
            //模板分类使用
            re = sql.Query("select top 1 mid,cont from [_mb_模板] where ktid='" + title.KTid + "' order by times asc");
            if (re.Read())
            {
                result = re["cont"].ToString();
                mid = re["mid"].ToString();
            }
            else
            {
                re.Close();
                return "";
            }
            re.Close();
            sql.Update("update [_mb_模板] set times=times+1 where mid='" + mid + "'");
            result = Regex.Replace(result, "</?font[\\s\\S]*?>", "", RegexOptions.IgnoreCase);
            //过滤tag标题
            result = Regex.Replace(result, "{tag:标题}", title.Word);
            //过滤tag疾病
            result = result.Replace("{tag:疾病}", ktype.Get(title.KTid));
            //标签替换
            MatchCollection mc = Regex.Matches(result, "{tag:[^}]+}");
            foreach (Match m in mc)
            {
                GroupCollection gc = m.Groups;
                string html = tag.Get(gc[0].Value);
                html = Regex.Replace(html, "^\\s*<p>", "", RegexOptions.IgnoreCase);
                html = Regex.Replace(html, "</p>\\s*$", "", RegexOptions.IgnoreCase);
                html = html.Trim();
                result = new Regex(gc[0].Value).Replace(result, html, 1);
            }
            //非法词过滤
            result = iw.Execute(result);
            return result;
        }
    }
}
